#!/bin/bash

# Copyright (C) 2019 Karel Zak <kzak@redhat.com>

TS_TOPDIR="${0%/*}/../.."
TS_DESC="all (fstab)"

. $TS_TOPDIR/functions.sh
ts_init "$*"

ts_check_test_command "$TS_CMD_MOUNT"
ts_check_test_command "$TS_CMD_UMOUNT"
ts_check_test_command "$TS_CMD_FDISK"

ts_skip_nonroot
ts_check_prog "mkfs.ext2"
ts_check_prog "mkfs.ext4"


# set global variable TS_DEVICE
ts_scsi_debug_init dev_size_mb=50

ts_log "Create partitions"
$TS_CMD_FDISK --noauto-pt ${TS_DEVICE} &> /dev/null <<EOF
o
n
p
1

+10M
n
p
2

+10M
n
p
3

+10M
n
p

+10M
w
q
EOF

udevadm settle

ts_log "Create filesystem A"
mkfs.ext2 ${TS_DEVICE}1 &> /dev/null
ts_log "Create filesystem B"
mkfs.ext2 ${TS_DEVICE}2 &> /dev/null
ts_log "Create filesystem C"
mkfs.ext2 ${TS_DEVICE}3 &> /dev/null
ts_log "Create filesystem D"
mkfs.ext4 ${TS_DEVICE}4 &> /dev/null

udevadm settle

# use the same top-level mountpoint for all sub-tests
MOUNTPOINT=$TS_MOUNTPOINT

[ -d "${MOUNTPOINT}A" ] || mkdir -p ${MOUNTPOINT}A
[ -d "${MOUNTPOINT}B" ] || mkdir -p ${MOUNTPOINT}B
[ -d "${MOUNTPOINT}C" ] || mkdir -p ${MOUNTPOINT}C
[ -d "${MOUNTPOINT}D" ] || mkdir -p ${MOUNTPOINT}D

# generic fstab
MY_FSTAB="$TS_OUTDIR/${TS_TESTNAME}.fstab"
rm -rf $MY_FSTAB
echo  "${TS_DEVICE}1 ${MOUNTPOINT}A ext2 rw,defaults 0 0" >> $MY_FSTAB 
echo  "${TS_DEVICE}2 ${MOUNTPOINT}B ext2 rw,defaults 0 0" >> $MY_FSTAB
echo  "${TS_DEVICE}3 ${MOUNTPOINT}C ext2 ro,defaults 0 0" >> $MY_FSTAB
echo  "${TS_DEVICE}4 ${MOUNTPOINT}D ext4 rw,defaults 0 0" >> $MY_FSTAB


ts_init_subtest "basic"
$TS_CMD_MOUNT --all --fstab $MY_FSTAB >> $TS_OUTPUT 2>> $TS_ERRLOG
[ $? == 0 ] || ts_log "mount failed"
udevadm settle
$TS_CMD_UMOUNT ${MOUNTPOINT}{A,B,C,D}
[ $? == 0 ] || ts_log "umount failed"
ts_finalize_subtest


ts_init_subtest "filter-type"
$TS_CMD_MOUNT --all --fstab $MY_FSTAB -t ext4 >> $TS_OUTPUT 2>> $TS_ERRLOG
[ $? == 0 ] || ts_log "mount failed"
udevadm settle
$TS_CMD_UMOUNT ${MOUNTPOINT}D
[ $? == 0 ] || ts_log "umount failed"
ts_finalize_subtest


ts_init_subtest "filter-notype"
$TS_CMD_MOUNT --all --fstab $MY_FSTAB -t noext4 >> $TS_OUTPUT 2>> $TS_ERRLOG
[ $? == 0 ] || ts_log "mount failed"
udevadm settle
$TS_CMD_UMOUNT ${MOUNTPOINT}{A,B,C}
[ $? == 0 ] || ts_log "umount failed"
ts_finalize_subtest


ts_init_subtest "filter-option"
$TS_CMD_MOUNT --all --fstab $MY_FSTAB -O ro >> $TS_OUTPUT 2>> $TS_ERRLOG
[ $? == 0 ] || ts_log "mount failed"
udevadm settle
$TS_CMD_UMOUNT ${MOUNTPOINT}C
[ $? == 0 ] || ts_log "umount failed"
ts_finalize_subtest


ts_init_subtest "override-option"
$TS_CMD_MOUNT --all --fstab $MY_FSTAB -o ro >> $TS_OUTPUT 2>> $TS_ERRLOG
[ $? == 0 ] || ts_log "mount failed"
udevadm settle
$TS_CMD_UMOUNT ${MOUNTPOINT}{A,B,C,D}
[ $? == 0 ] || ts_log "umount failed"
ts_finalize_subtest


ts_init_subtest "prefix"
MY_ROOT="$TS_OUTDIR/${TS_TESTNAME}-rootdir"
[ -d "${MY_ROOT}" ] || mkdir -p ${MY_ROOT}
# sub-test specific fstab
MY_FSTAB="$TS_OUTDIR/${TS_TESTNAME}.fstab"
rm -rf $MY_FSTAB
echo  "${TS_DEVICE}1 /foo/A ext2 rw,defaults 0 0" >> $MY_FSTAB
echo  "${TS_DEVICE}2 /foo/B ext2 rw,defaults 0 0" >> $MY_FSTAB
echo  "${TS_DEVICE}3 /foo/C ext2 rw,defaults 0 0" >> $MY_FSTAB
echo  "${TS_DEVICE}4 /foo/D ext4 rw,defaults 0 0" >> $MY_FSTAB
# mount all to prefix
$TS_CMD_MOUNT	--all \
		--fstab $MY_FSTAB \
		--target-prefix $MY_ROOT \
		-o X-mount.mkdir >> $TS_OUTPUT 2>> $TS_ERRLOG
[ $? == 0 ] || ts_log "mount failed"
udevadm settle
$TS_CMD_UMOUNT $MY_ROOT/foo/{A,B,C,D}
[ $? == 0 ] || ts_log "umount failed"
ts_finalize_subtest

ts_finalize
